package com.magicsoft.share.adapter.share;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;

import com.magicsoft.share.bean.share.ArticleType;
import com.magicsoft.share.ui.share.ArticleListFragment;
import com.magicsoft.share.ui.share.HotSpotArticleFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyf on 2017/10/25/025.
 */

public class ShareFragmentAdapter extends FragmentPagerAdapter {
    private final HotSpotArticleFragment hotSpotArticleFragment;
    private List<Fragment> mFragments = new ArrayList<>();
    private List<ArticleType> mArticleTypeList = new ArrayList<>();

    public ShareFragmentAdapter(FragmentManager fm) {
        super(fm);
        hotSpotArticleFragment = new HotSpotArticleFragment();
        mFragments.add(hotSpotArticleFragment);
        mFragments.add(new ArticleListFragment());
        mFragments.add(new ArticleListFragment());
        mFragments.add(new ArticleListFragment());
        mFragments.add(new ArticleListFragment());
        mFragments.add(new ArticleListFragment());
        mFragments.add(new ArticleListFragment());
        mFragments.add(new ArticleListFragment());
        mFragments.add(new ArticleListFragment());
        mFragments.add(new ArticleListFragment());
        mFragments.add(new ArticleListFragment());
        mFragments.add(new ArticleListFragment());
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mArticleTypeList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mArticleTypeList.get(position).getType_name();
    }

    public void refreshData(List<ArticleType> articleTypeList, int selectedTabPosition) {
        for (int i = 0; i < mFragments.size(); i++) {
            if (i == 0) {
                HotSpotArticleFragment hotSpotArticleFragment = (HotSpotArticleFragment) mFragments.get(i);

                if (selectedTabPosition == i) {
                    Log.d("luf", "selectPosition: " + selectedTabPosition + "," + i);
                    hotSpotArticleFragment.refreshData();
                }
            } else {

                ArticleListFragment articleListFragment = (ArticleListFragment) mFragments.get(i);
                articleListFragment.setType(articleTypeList.get(i).getId());
                if (selectedTabPosition == i) {
                    Log.d("luf", "selectPosition: " + selectedTabPosition + "," + i);
                    articleListFragment.rrefreshData();
                }

            }

        }

        mArticleTypeList = articleTypeList;
        this.notifyDataSetChanged();
    }

    public void setData(List<ArticleType> articleTypeList) {

        mArticleTypeList = articleTypeList;
        for (int i = 0; i < mFragments.size(); i++) {
            if (i == 0) {
                HotSpotArticleFragment hotSpotArticleFragment = (HotSpotArticleFragment) mFragments.get(i);

            } else {

                ArticleListFragment articleListFragment = (ArticleListFragment) mFragments.get(i);
                articleListFragment.setType(articleTypeList.get(i).getId());

            }

        }
        this.notifyDataSetChanged();
    }
}
